/*
 * *****************************************************************************************
 *
 * 		Copyright (C) 2014-2019 Gowin Semiconductor Technology Co.,Ltd.
 * 		
 * @file			main.c
 * @author		Embedded Development Team
 * @version		V1.0.0
 * @date			2019-10-1 09:00:00
 * @brief			Main function.
 ******************************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "gw1ns4c.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

void UartInit(void);

int main()
{
  NVIC_InitTypeDef InitTypeDef_NVIC;

  SystemInit();	//init system
  UartInit();		//init uart

  InitTypeDef_NVIC.NVIC_IRQChannel = UART0_IRQn;
  InitTypeDef_NVIC.NVIC_IRQChannelPreemptionPriority = 1;
  InitTypeDef_NVIC.NVIC_IRQChannelSubPriority = 1;
  InitTypeDef_NVIC.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&InitTypeDef_NVIC);
	
  printf("/************GOWINSEMI*****************/\r\n");
  printf("           GOWIN_EMPU_M1                \r\n");
  printf("/**************************************/\r\n");

  while(1);
}

void UartInit(void)
{
  UART_InitTypeDef UART_InitStruct;
	
  UART_InitStruct.UART_Mode.UARTMode_Tx = ENABLE;
  UART_InitStruct.UART_Mode.UARTMode_Rx = ENABLE;
  UART_InitStruct.UART_Int.UARTInt_Tx = DISABLE;
  UART_InitStruct.UART_Int.UARTInt_Rx = ENABLE;//enable uart0 rx interrupt register
  UART_InitStruct.UART_Ovr.UARTOvr_Tx = DISABLE;
  UART_InitStruct.UART_Ovr.UARTOvr_Rx = DISABLE;
  UART_InitStruct.UART_Hstm = DISABLE;
  UART_InitStruct.UART_BaudRate = 115200;//Baud Rate
	
  UART_Init(UART0,&UART_InitStruct);
}

/**
  * @brief  This function handles UART0 interrupt request.
  * @param  none
  * @retval none
  */
void UART0_Handler(void)
{
	char num = '0';
	
	if(UART_GetRxIRQStatus(UART0) == SET)
	{
		num  = UART_ReceiveChar(UART0);
		printf("UART0 receives a Rx interrupt value %c\r\n",num);
		
		GPIO0->OUTENSET = 0xffffffff;
		
		if(num == '1')
		{
			GPIO0->DATAOUT = 0xe;
		}
		else if(num == '2')
		{
			GPIO0->DATAOUT = 0xd;
		}
		else if(num == '3')
		{
			GPIO0->DATAOUT = 0xb;
		}
		else if(num == '4')
		{
			GPIO0->DATAOUT = 0x7;
		}
		else
		{
			printf("No operation!\r\n");
		}
	}
	
	UART_ClearRxIRQ(UART0);
}
